package com.frxuexi.sign

import com.frxuexi.lib_base.http.ApiService
import com.frxuexi.lib_base.http.RequestType
import com.frxuexi.lib_base.mvi.BaseViewModel
import com.frxuexi.lib_base.room.SignDao
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.flow
import javax.inject.Inject

@HiltViewModel
class SignViewModel @Inject constructor(val apiService: ApiService,val signDao: SignDao):BaseViewModel<SignIntent>(){
    override fun handleIntent(it: SignIntent) {
        when(it){
            is SignIntent.getSings->httpRequest(apiService.getrSigns(it.uid))
            is SignIntent.sign->httpRequest(apiService.sign(toBody(it.item)),RequestType.ADD)
            is SignIntent.insertSign->roomRequest(flow { emit(signDao.addSign(it.signEntity)) },RequestType.ADD)
            is SignIntent.updateSign->roomRequest(flow { emit(signDao.updateSign(it.signEntity)) },RequestType.UPDATE)
            is SignIntent.selectSigByDate->roomRequest(flow { emit(signDao.selectByDate(it.date)) })
            is SignIntent.selectSigByDate->roomRequest(flow { emit(signDao.selectByDate(it.date)) })
        }
    }
}